package tom.client.invoice;

import java.util.List;
import java.util.Map;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.client.MainPanel;
import tom.shared.Contact;
import tom.shared.ContactGroup;
import tom.shared.PostCode;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;

public class FormContact extends DataForm {
	private static FormContact panel;

	public static FormContact getPanel() {
		if (panel == null)
			panel = GWT.create(FormContact.class);
		return panel;
	}

	private final TextBox txtName;
	private final TextBox txtOrganiz;
	private final TextBox txtAddress;
	private final TextBox txtAmphur;
	private final TextBox txtProvince;
	private final TextBox txtPostCode;
	private final TextBox txtPhone;
	private final TextBox txtFax;
	private final TextBox txtMobile;
	private final TextBox txtEmail;

	private Contact record;

	public FormContact() {
		setSize("900px", "500px");
		txtCode.setMaxLength(6);

		txtName = new TextBox();
		txtName.setMaxLength(40);
		add(txtName, 167, 106);
		txtName.setSize("253px", "18px");

		txtOrganiz = new TextBox();
		txtOrganiz.setMaxLength(70);
		add(txtOrganiz, 122, 138);
		txtOrganiz.setSize("298px", "18px");

		txtAddress = new TextBox();
		txtAddress.setMaxLength(100);
		txtAddress.setVisibleLength(50);
		add(txtAddress, 122, 202);
		txtAddress.setSize("298px", "18px");

		btnSearchPostCode = new Button("?");
		btnSearchPostCode.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("รหัสไปรษณีย์", "postCode", txtIdPostCode);
			}
		});
		add(btnSearchPostCode, 196, 266);
		btnSearchPostCode.setSize("25px", "25px");
		btnSearchPostCode.setTitle("ค้นหารหัสไปรษณีย์ อำเภอ จังหวัด");

		txtAmphur = new TextBox();
		add(txtAmphur, 320, 297);
		txtAmphur.setMaxLength(20);
		txtAmphur.setSize("100px", "18px");

		txtProvince = new TextBox();
		txtProvince.setMaxLength(20);
		add(txtProvince, 122, 297);
		txtProvince.setSize("100px", "18px");

		txtPostCode = new TextBox();
		txtPostCode.setMaxLength(5);
		add(txtPostCode, 122, 266);
		txtPostCode.setSize("60px", "18px");

		Label lblNewLabel = new Label("ชื่อ");
		add(lblNewLabel, 23, 110);

		Label label = new Label("สถานประกอบการ");
		add(label, 23, 143);
		label.setSize("98px", "21px");

		Label label_2 = new Label("ที่อยู่ เพิ่มเติม");
		add(label_2, 23, 234);
		label_2.setSize("93px", "21px");

		Label label_3 = new Label("อำเภอ");
		add(label_3, 258, 297);
		label_3.setSize("93px", "21px");

		Label label_4 = new Label("จังหวัด");
		add(label_4, 23, 297);
		label_4.setSize("93px", "21px");

		Label label_5 = new Label("รหัสไปรษณีย์");
		add(label_5, 23, 266);
		label_5.setSize("93px", "21px");

		txtPhone = new TextBox();
		txtPhone.setMaxLength(40);
		add(txtPhone, 122, 329);
		txtPhone.setSize("123px", "18px");

		txtFax = new TextBox();
		txtFax.setMaxLength(40);
		add(txtFax, 122, 361);
		txtFax.setSize("180px", "18px");

		txtMobile = new TextBox();
		txtMobile.setMaxLength(40);
		add(txtMobile, 320, 329);
		txtMobile.setSize("133px", "18px");

		txtEmail = new TextBox();
		txtEmail.setMaxLength(40);
		add(txtEmail, 122, 393);
		txtEmail.setSize("237px", "18px");

		Label label_6 = new Label("โทรศัพท์");
		add(label_6, 23, 332);
		label_6.setSize("93px", "21px");

		Label lblFax = new Label("FAX");
		add(lblFax, 23, 361);
		lblFax.setSize("93px", "21px");

		Label label_8 = new Label("มือถือ");
		add(label_8, 259, 332);
		label_8.setSize("39px", "21px");

		Label lblEmail = new Label("E-Mail");
		add(lblEmail, 23, 398);
		lblEmail.setSize("64px", "21px");

		txtIdPostCode = new TextBox();
		txtIdPostCode.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					Library.getAdminService().selectPostCode(
							Integer.valueOf(txtIdPostCode.getText()),
							postCodeCallback);
				}
			}
		});
		// txtIdPostCode.addKeyDownHandler(new KeyDownHandler() {
		// public void onKeyDown(KeyDownEvent event) {
		// if (event.getCharCode() == KeyCodes.KEY_ENTER) {
		// Library.getAdminService().selectPostCode(
		// Integer.valueOf(txtIdPostCode.getText()),
		// postCodeCallback);
		// }
		// }
		// });
		txtIdPostCode.setVisible(false);
		add(txtIdPostCode, 580, 138);
		txtIdPostCode.setSize("56px", "16px");

		txtNote = new TextArea();
		add(txtNote, 518, 169);
		txtNote.setSize("270px", "80px");

		Label label_9 = new Label("หมายเหตุ");
		add(label_9, 444, 169);
		label_9.setSize("68px", "21px");

		groupBox = new ListBox();
		add(groupBox, 520, 266);
		groupBox.setSize("128px", "80px");
		groupBox.setVisibleItemCount(5);

		Label label_1 = new Label("กลุ่ม");
		add(label_1, 450, 269);
		label_1.setSize("46px", "21px");

		btnAddGroup = new Button("เพิ่มกลุ่ม");
		btnAddGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("ประเภท ที่ติดตอ่", "grpId", txtGrpId);
			}
		});
		add(btnAddGroup, 668, 266);
		btnAddGroup.setSize("64px", "24px");

		btnDelGroup = new Button("ลบกลุ่ม");
		btnDelGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int x = groupBox.getSelectedIndex();
				if (x > -1) {
					if (Window.confirm("ต้องการลบกลุ่ม "
							+ groupBox.getItemText(x) + " หรือไม่?")) {
						Library.getSaleService().deleteContGroup(
								txtCode.getText(),
								Integer.valueOf(groupBox.getValue(x)),
								delGrpCallback);
					}
				}
			}
		});
		add(btnDelGroup, 668, 322);
		btnDelGroup.setSize("64px", "24px");

		lbModUserTime = new Label("...");
		lbModUserTime.setWordWrap(false);
		add(lbModUserTime, 270, 74);
		lbModUserTime.setSize("335px", "21px");

		txtGrpId = new TextBox();
		txtGrpId.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					if (txtGrpId != null && !txtGrpId.equals("")) {
						Library.getSaleService().insertContGroup(
								txtCode.getText(),
								Integer.valueOf(txtGrpId.getText()),
								delGrpCallback);
					}
				}
			}
		});

		txtGrpId.setVisible(false);
		add(txtGrpId, 668, 292);
		txtGrpId.setSize("64px", "16px");

		txtSurname = new TextBox();
		txtSurname.setMaxLength(40);
		add(txtSurname, 434, 106);
		txtSurname.setSize("253px", "18px");

		txtPrefix = new TextBox();
		txtPrefix.setMaxLength(40);
		add(txtPrefix, 122, 106);
		txtPrefix.setSize("31px", "18px");

		txtBranch = new TextBox();
		txtBranch
				.setText("                                                                                                                     ");
		txtBranch.setMaxLength(40);
		add(txtBranch, 518, 138);
		txtBranch.setSize("52px", "18px");

		txtLabel = new TextArea();
		add(txtLabel, 518, 361);
		txtLabel.setSize("329px", "91px");

		lblx = new Label("พิมพ์ชื่อที่อยู่");
		add(lblx, 419, 361);
		lblx.setSize("68px", "26px");

		txtTambon = new TextBox();
		txtTambon.setMaxLength(20);
		add(txtTambon, 320, 267);
		txtTambon.setSize("100px", "18px");

		label_11 = new Label("ตำบล");
		add(label_11, 258, 266);
		label_11.setSize("46px", "21px");

		txtAddLine = new TextBox();
		txtAddLine.setVisibleLength(50);
		txtAddLine.setMaxLength(100);
		add(txtAddLine, 122, 234);
		txtAddLine.setSize("298px", "18px");

		label_12 = new Label("บ้านเลขที่");
		add(label_12, 23, 202);
		label_12.setSize("93px", "21px");

		txtTaxId = new TextBox();
		txtTaxId.setMaxLength(40);
		add(txtTaxId, 122, 170);
		txtTaxId.setSize("237px", "18px");

		Label lblNewLabel_1 = new Label("เลขผู้เสียภาษี");
		add(lblNewLabel_1, 23, 174);

		Button btnNewButton = new Button(">>");
		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (Window.confirm("ต้องการสร้างลาเบลที่อยู่หรือไม่ ?")) {
					FormContact.this.setMode(2);
					txtLabel.setText(txtPrefix.getText() + txtName.getText());
					if (!txtSurname.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + " "
								+ txtSurname.getText());
					if (txtBranch.getText().equals("00000")
							|| txtBranch.getText().equals(""))
						txtLabel.setText(txtLabel.getText()
								+ " (สำนักงานใหญ่)\n");
					else
						txtLabel.setText(txtLabel.getText() + " (สาขาที่ "
								+ txtBranch.getText() + ")\n");
					if (!txtAddLine.getText().equals(""))
						txtLabel.setText(txtLabel.getText()
								+ txtAddLine.getText() + " ");
					if (!txtAddress.getText().equals(""))
						txtLabel.setText(txtLabel.getText()
								+ txtAddress.getText());
					if (!txtTambon.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + " ต."
								+ txtTambon.getText());
					txtLabel.setText(txtLabel.getText() + "\n");
					if (!txtAmphur.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + "อ."
								+ txtAmphur.getText() + " ");
					if (!txtProvince.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + "จ."
								+ txtProvince.getText() + " ");
					if (!txtPostCode.getText().equals(""))
						txtLabel.setText(txtLabel.getText()
								+ txtPostCode.getText());
					txtLabel.setText(txtLabel.getText() + "\n");
					if (!txtTaxId.getText().equals(""))
						txtLabel.setText(txtLabel.getText()
								+ "เลขประจำตัวผู้เสียภาษี " + txtTaxId.getText());

					if (!txtPhone.getText().equals("")
							|| !txtMobile.getText().equals("")
							|| !txtFax.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + "\n");
					if (!txtPhone.getText().equals("")) {
						if (!txtPhone.getText().equals("")) {
							txtLabel.setText(txtLabel.getText()
									+ "โทร." + txtPhone.getText() + " ");
						}

					} else {
						if (!txtMobile.getText().equals("")) {
							txtLabel.setText(txtLabel.getText()
									+ "โทร." + txtMobile.getText() + " ");
						}
					}

					if (!txtFax.getText().equals(""))
						txtLabel.setText(txtLabel.getText() + "FAX "
								+ txtFax.getText());

					// + txtAddress.getText() + " " + txtTambon.getText()
					// + "\n" + txtAmphur.getText() + " "
					// + txtProvince.getText() + " "
					// + txtPostCode.getText());
				}
			}
		});
		btnNewButton.setText(">>");
		add(btnNewButton, 486, 361);

		label_13 = new Label("สาขาที่");
		add(label_13, 444, 143);
		label_13.setSize("46px", "21px");

		Button button = new Button("ฝ่ายที่ติดต่อ");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				MainPanel.mainPanel.show(FormAddrDept.getPanel(),
						"ฝ่ายที่ติดต่อ");
				FormAddrDept.getPanel().open(record.contCode);
			}
		});
		add(button, 629, 74);
		this.setFieldsHandler();
	}

	@Override
	protected void search() {
		LookupDialog.show("ที่อยู่/ที่ติดต่อ", "contCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getSaleService().selectContact(txtCode.getText(), dataCallback);
		Library.getSaleService()
				.selectContGroup(txtCode.getText(), grpCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getSaleService().updateContact(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getSaleService().insertContact(record, delCallback);
	}

	@Override
	protected void delete() {
		if (Window.confirm("ต้องการลบที่อยู่/ที่ติดต่อ" + txtName.getText()
				+ "  " + txtOrganiz.getText() + " หรือไม่")) {
			Library.getSaleService().deleteContact(txtCode.getText(),
					delCallback);
		}

	}

	@Override
	protected void clearFields() {
		// txtCode.setText(null);
		super.clearFields();
		txtPrefix.setText(null);
		txtName.setText(null);
		txtSurname.setText(null);
		txtTaxId.setText(null);
		txtOrganiz.setText(null);
		txtBranch.setText(null);
		txtAddress.setText(null);
		txtAddLine.setText(null);
		txtTambon.setText(null);
		txtAmphur.setText(null);
		txtProvince.setText(null);
		txtPostCode.setText(null);
		txtPhone.setText(null);
		txtFax.setText(null);
		txtMobile.setText(null);
		txtEmail.setText(null);
		// chkActive.setValue(null);
		txtNote.setText(null);
		txtLabel.setText(null);
		groupBox.clear();
		lbModUserTime.setText(null);
		btnSearchPostCode.setEnabled(false);
		btnAddGroup.setEnabled(false);
		btnDelGroup.setEnabled(false);
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtCode.setText(record.contCode);
			txtPrefix.setText(record.prefix);
			txtName.setText(record.name);
			txtSurname.setText(record.surname);
			txtTaxId.setText(record.taxId);
			txtOrganiz.setText(record.organiz);
			txtBranch.setText(record.branch);
			txtAddress.setText(record.address);
			txtAddLine.setText(record.addLine);
			txtTambon.setText(record.tambon);
			txtAmphur.setText(record.amphur);
			txtProvince.setText(record.province);
			txtPostCode.setText(record.postCode);
			txtPhone.setText(record.phone);
			txtFax.setText(record.fax);
			txtMobile.setText(record.mobile);
			txtEmail.setText(record.email);
			// chkActive.setValue(new Boolean(true));
			txtNote.setText(record.note);
			txtLabel.setText(record.label);
			// lbModUserTime.setText("แก้ไขล่าสุด " + record.creUser + " "
			// + Library.DATETIME_FORMAT.format(record.modTime));
			btnSearchPostCode.setEnabled(true);
			btnAddGroup.setEnabled(true);
			btnDelGroup.setEnabled(true);
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}
	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new Contact();
		record.contCode = txtCode.getText();
		record.prefix = txtPrefix.getText();
		record.name = txtName.getText();
		record.surname = txtSurname.getText();
		record.organiz = txtOrganiz.getText();
		record.branch = txtBranch.getText();
		record.address = txtAddress.getText();
		record.addLine = txtAddLine.getText();
		record.tambon = txtTambon.getText();
		record.amphur = txtAmphur.getText();
		record.province = txtProvince.getText();
		record.postCode = txtPostCode.getText();
		record.phone = txtPhone.getText();
		record.fax = txtFax.getText();
		record.mobile = txtMobile.getText();
		record.email = txtEmail.getText();
		record.active = new Boolean(true);
		record.note = txtNote.getText();
		record.label = txtLabel.getText();
		record.taxId = txtTaxId.getText();
	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	AsyncCallback<PostCode> postCodeCallback = new AsyncCallback<PostCode>() {

		@Override
		public void onSuccess(PostCode result) {
			if (result != null) {
				txtAmphur.setText(result.amphur);
				txtProvince.setText(result.province);
				txtPostCode.setText(result.postCode);
			} else {
				txtAmphur.setText(null);
				txtProvince.setText(null);
				txtPostCode.setText(null);
			}
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<Contact> dataCallback = new AsyncCallback<Contact>() {

		@Override
		public void onSuccess(Contact result) {
			record = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<Boolean> delGrpCallback = new AsyncCallback<Boolean>() {

		@Override
		public void onSuccess(Boolean result) {
			Library.getSaleService().selectContGroup(txtCode.getText(),
					grpCallback);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<List<ContactGroup>> grpCallback = new AsyncCallback<List<ContactGroup>>() {

		@Override
		public void onSuccess(List<ContactGroup> result) {
			groupBox.clear();
			groupBox.setVisible(false);
			for (int i = 0; i < result.size(); i++)
				groupBox.addItem(result.get(i).grpName,
						result.get(i).grpId.toString());
			groupBox.setVisible(true);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	private final TextBox txtIdPostCode;
	private final Button btnSearchPostCode;
	private final TextArea txtNote;
	private final Label lbModUserTime;
	private final Button btnDelGroup;
	private final Button btnAddGroup;
	private final ListBox groupBox;
	private final TextBox txtGrpId;
	private TextBox txtSurname;
	private TextBox txtPrefix;
	private TextBox txtBranch;
	private TextArea txtLabel;
	private Label lblx;
	private TextBox txtTambon;
	private Label label_11;
	private TextBox txtAddLine;
	private Label label_12;
	private TextBox txtTaxId;
	private Label label_13;
}
